package ru.spb.xmlreshelper.screen.files;

import org.apache.commons.lang3.StringUtils;
import ru.spb.xmlreshelper.util.GuiUtils;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;

abstract class UpdateDownloadFilePanel extends JPanel {
    private final String updateEventKey;
    private final String title;
    private JTextField pathField;
    private String extensionFilter;
    private JLabel resultLabel;


    public UpdateDownloadFilePanel(String updateEventKey, String title) {
        this.updateEventKey = updateEventKey;
        this.title = title;
        build();
    }

    private void build() {
        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createTitledBorder(title));
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        pathField = new JTextField("");
        c.gridx = 0;
        c.weightx = 0.9;
        add(pathField, c);
        c.gridx = 1;
        c.weightx = 0.05;
        add(new JButton(new AbstractAction() {
            {
                putValue(AbstractAction.NAME, "Select");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                final JFileChooser fileChooser = new JFileChooser(getLastUsedDirectory());
                fileChooser.setMultiSelectionEnabled(false);
                fileChooser.setFileFilter(new FileNameExtensionFilter("'." + extensionFilter + "' files", extensionFilter));
                if (fileChooser.showOpenDialog(UpdateDownloadFilePanel.this) == JFileChooser.APPROVE_OPTION) {
                    pathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
                }
            }
        }), c);
        c.gridx = 2;
        c.weightx = 0.15;
        c.ipady = 5;
        add(new JButton(new AbstractAction() {
            {
                putValue(AbstractAction.NAME, "Upload");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                resultLabel.setForeground(Color.BLUE);
                resultLabel.setText("Uploading...");
                SwingWorker sw = new SwingWorker() {

                    private boolean success = false;

                    @Override
                    protected Object doInBackground() throws Exception {

                        try {
                            performUpload(getPath());
                            success = true;
                        } catch (Exception e) {
                            //TODO dialog
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void done() {
                        if (success) {
                            resultLabel.setForeground(Color.GREEN);
                            resultLabel.setText("Successfully uploaded");
                        } else {
                            resultLabel.setForeground(Color.RED);
                            resultLabel.setText("Error occurred");
                        }
                    }
                };
                sw.execute();

                GuiUtils.getEventBus().post(new FileUpdateCommandEvent(updateEventKey, getPath()));
            }
        }), c);
        c.gridy = 1;
        c.gridx = 0;
        c.weightx = 1.0;
        c.gridwidth = GridBagConstraints.RELATIVE;
        resultLabel = new JLabel("");
        resultLabel.setHorizontalTextPosition(JLabel.CENTER);
        add(resultLabel, c);
    }

    public String getPath() {
        return pathField.getText();
    }

    public void setExtensionFilter(String extensionFilter) {
        this.extensionFilter = extensionFilter;
    }

    private String getLastUsedDirectory() {
        if (StringUtils.isNotEmpty(getPath())) {
            return GuiUtils.getFileDirectory(getPath());
        } else
            //TODO from configuration
            return "";
    }

    protected abstract void performUpload(String filePath);

    private class DownloadAction extends AbstractAction{

        {
            putValue(AbstractAction.NAME, "Download");
        }

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    }
}